package nl.hu.verbrandendephoenix.controller;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.Auction;
import nl.hu.verbrandendephoenix.shared.FieldVerifier;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;

public class ViewProduct {
	
	private nl.hu.verbrandendephoenix.view.ViewProduct viewProduct;
	private int acID = 0;

	public ViewProduct() {
		
	}
	
	public Panel getPanel(int auctionID) {
		viewProduct = new nl.hu.verbrandendephoenix.view.ViewProduct();
		Panel p = viewProduct.constructForm();
		getData(auctionID);
		addHandlers();
		acID = auctionID;
		return p;
	}
	
	private void setData(Auction a) {
		viewProduct.setLblProductName(new Label("Veilingnaam: " + a.getAuctionName()));
		viewProduct.setLblDescription(new Label("Omschrijving: " + a.getDescription()));
		viewProduct.setLblPrice(new Label("Prijs: " + NumberFormat.getCurrencyFormat("EUR").format(a.getIniPrice())));
		viewProduct.setLblCategory(new Label("Categorie: " + a.getCategory()));
		viewProduct.setLblOwner(new Label("Aanbieder: " + a.getUserID()));
		viewProduct.setLblCurrentOffer(new Label("Huidige bod: ")); //+ p.getCurrentOffer()));
		viewProduct.setLblStartDate(new Label("Aanmaakdatum: " + a.getEndDate()));
		a.getEndDate().setMonth(a.getEndDate().getMonth() + 1);
		viewProduct.setLblEndDate(new Label("Einddatum: " + a.getEndDate()));
	}

	public void getData(int auctionID) {
		final NotificationManager nM = NotificationManager.get();
		
		AppManager.get().getRpcService().getAuctionData(auctionID, new AsyncCallback<Auction>() {
			public void onFailure(Throwable caught) {
				nM.commError();
			}
			public void onSuccess(Auction result) {
				if(result != null) {
					setData(result);
				} else {
					nM.createNotification("Veiling bestaat niet meer.");
				}
			}
			
		});
	}
	
	private void processSubmit() {
		final NotificationManager nM =  NotificationManager.get();

		if(!checkFields()) {
			nM.createNotification("Er is iets mis gegaan!");
		} else {
			nM.createNotification("Submit is aangeroepen");
		}
		
		AppManager.get().getRpcService().bidAuction(acID, Double.parseDouble(viewProduct.getTxtOffer().getText()), Cookies.getCookie("loginString"), new AsyncCallback<Boolean>() {

			public void onFailure(Throwable caught) {
				Window.alert("Communication Failure!");
			}

			public void onSuccess(Boolean result) {
				if(result) {
					nM.createNotification("Bod is succesvol opgeslagen");
					viewProduct.setLblCurrentOffer(new Label("Huidige bod: " + NumberFormat.getCurrencyFormat("EUR").format(Double.parseDouble(viewProduct.getTxtOffer().getText()))));
					viewProduct.setTxtOffer("");
				} 
				else {
					Window.alert("Bod is lager dan de huidige bod!");
				}
			}
		});
	}
	
	private boolean checkFields() {
		FieldVerifier fv = FieldVerifier.get();
		boolean b = true; 

		TextBox txtOffer = viewProduct.getTxtOffer();
		if(!fv.checkDouble(txtOffer.getText())) {
			fv.addRequired(txtOffer);
			b = false;
		} else {
			fv.removeRequired(txtOffer);
		}

		return b;
	}
	
	public void addHandlers() {
		Button btnBid = viewProduct.getBtnBid();
		btnBid.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				processSubmit();
			} 
		});
		
		Button btnCancel = viewProduct.getBtnCancel();
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
			} 
		});

		ArrayList<TextBox> arrTextBoxes = viewProduct.getArrTextBox();
		for(TextBox tb : arrTextBoxes) {
			tb.addKeyPressHandler(new KeyPressHandler() {
				@Override
				public void onKeyPress(KeyPressEvent event) {
					if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
						processSubmit();
					}
				}
			});
		}
	}
}
